package servlet;

import domain.Product;
import org.apache.commons.beanutils.BeanUtils;
import service.impl.ProdutcServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Author admin
 * @Date 2019/2/28 11:22
 */
@WebServlet(name = "AddServlet" ,urlPatterns = "/addServlet")
public class AddServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProdutcServiceImpl service = new ProdutcServiceImpl();
        Map<String, String[]> map = request.getParameterMap();
        Product product = new Product();
        try {
            BeanUtils.populate(product,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
         service.addProducct(product);
         response.sendRedirect("/findAll");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
